<template>
  <!-- 车辆进出记录 -->
  <div class="vehicleentryexitrecord">
    <basic-container>
      <avue-crud
        :option="option"
        :table-loading="loading"
        :data="data"
        :page.sync="page"
        :search.sync="searchObj"
        v-model="form"
        ref="crud"
        @search-change="searchChange"
        @search-reset="searchReset"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
      >

        <!-- 开始时间搜索栏自定义 -->
        <template slot="startTimeSearch">
          <el-date-picker
            v-model="searchObj.startTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="pickerOption"
            size="small"
            type="datetime"
            placeholder="选择开始时间">
          </el-date-picker>
        </template>

        <!-- 结束时间搜索栏自定义 -->
        <template slot="endTimeSearch">
          <el-date-picker
            v-model="searchObj.endTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="pickerOption"
            size="small"
            type="datetime"
            placeholder="选择结束时间">
          </el-date-picker>
        </template>
        
        <!-- 左上方菜单内容 -->
        <template slot="menuLeft">
          <el-button
            type="warning"
            size="small"
            plain
            icon="el-icon-download"
            @click="handleExport">
            导 出
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { getList } from "@/api/useronduty/vehicleentryexitrecord";
import { mapGetters } from "vuex";
import { getToken } from "@/util/auth";

export default {
  data() {
    return {
      searchObj: {
        startTime: this.formatStartTime(),
        endTime: this.formatEndTime()
      },
      pickerOption: {
        firstDayOfWeek: 1,
        disabledDate(time) {
          return time.getTime() > Date.now();
        }
      },
      form: {},
      data: [],
      loading: true,
      page: {
        pageSize: this.$pageSize,
        currentPage: 1,
        total: 0
      },
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchSpan: 5,
        searchMenuSpan: 4,
        border: true,
        index: false,
        menu: false,
        addBtn: false,
        selection: false,
        refreshBtn: true,
        columnBtn: false,
        dialogClickModal: false,
        column: [
          {
            label: "开始时间",
            prop: "startTime",
            searchslot: true,
            hide: true,
            search: true
          },
          {
            label: "结束时间",
            prop: "endTime",
            searchslot: true,
            hide: true,
            search: true
          },
          {
            label: "车牌号码",
            prop: "cphm",
            align: "center",
            search: true,
          },
          {
            label: "停车场名称",
            prop: "tccmc",
            align: "center"
          },
          {
            label: "时间",
            prop: "sj",
            align: "center"
          },
          {
            label: "方向",
            prop: "fx",
            align: "center"
          },
          {
            label: "车身颜色",
            prop: "csys",
            align: "center"
          }
        ]
      }
    };
  },
  watch: {
    // 开始时间
    "searchObj.startTime"() {
      let startDate = new Date(this.searchObj.startTime);
      let endDate = new Date(this.searchObj.endTime);
      if (startDate.getTime() > endDate.getTime()) {
        this.searchObj.endTime = this.searchObj.startTime;
      }
    },

    // 结束时间
    "searchObj.endTime"() {
      let startDate = new Date(this.searchObj.startTime);
      let endDate = new Date(this.searchObj.endTime);
      if (startDate.getTime() > endDate.getTime()) {
        this.searchObj.startTime = this.searchObj.endTime;
      }
    }
  },
  computed: {
    ...mapGetters(["permission"])
  },
  mounted() {
    this.onLoad(this.page);
  },
  methods: {
    
    // 获取开始时间
    formatStartTime() {
      let date = new Date(new Date().getTime() - 86400000 * 7);
      var year = date.getFullYear();
      var month = this.getFullNum(date.getMonth() + 1);
      var day = this.getFullNum(date.getDate());
      return year + "-" + month + "-" + day + " 00:00:00";
    },

    // 获取结束时间
    formatEndTime() {
      let date = new Date();
      var year = date.getFullYear();
      var month = this.getFullNum(date.getMonth() + 1);
      var day = this.getFullNum(date.getDate());
      return year + "-" + month + "-" + day + " 23:59:59";
    },

    // 补全时间
    getFullNum(num) {
      if (num < 10) {
        return "0" + num;
      }
      return num;
    },

    // 点击导出按钮
    handleExport() {
      this.$confirm("是否导出车辆进出记录?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let startTime =
          this.searchObj.startTime === undefined || this.searchObj.startTime === null
            ? ""
            : this.searchObj.startTime;
        let endTime =
          this.searchObj.endTime === undefined || this.searchObj.endTime === null
            ? ""
            : this.searchObj.endTime;
        let cphm =
          this.searchObj.cphm === undefined || this.searchObj.cphm === null
            ? ""
            : this.searchObj.cphm;
        window.open(
          `/api/entryAndExitRecords/vehicleentryexitrecord/export?${
            this.website.tokenHeader
          }=${getToken()}&startTime=${startTime}&endTime=${endTime}&cphm=${cphm}`
        );
      });
    },
    
    // 清空搜索条件
    searchReset() {
      this.searchObj = {};
      this.onLoad(this.page);
    },

    // 搜索条件改变事件
    searchChange(params, done) {
      this.searchObj = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },

    // 换页数
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },

    // 换每页显示数量
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },

    // 刷新
    refreshChange() {
      this.onLoad(this.page, this.searchObj);
    },

    // 获取表格数据
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.searchObj)
      ).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style lang="scss" scpoed>
.vehicleentryexitrecord {
  .avue-crud {
    .avue-crud__search {
      .avue-form__row:nth-child(1) {
        .el-form-item__label {
          text-align: unset;
        }
      }
    }
  }
}
</style>